package com.candy.service.impl;

import com.candy.dao.AccountDao;
import com.candy.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Date;

/**
 * 根据xml配置内容：
 * <bean id="accountService" class="com.candy.service.impl.AccountServiceImpL"
 *   scope=" “init-method=" " destroy-method="">
 *        <property name=""value="" / ref="“></ property>
 * < /bean>
 *     根据配置内容，可以将配置分为四类
 *     1、关于对象创建  相当于<bean></bean>标签的作用
 *     2、进行依赖注入的 相当于<property></property>标签的作用
 *     3、与作用范围相关的 相当于scope属性的作用
 *     4、根生命周期相关的 相当于init-method、destroy-method属性的作用
 *
 */
@Service
//@Scope("prototype")
@Transactional
public class AccountServiceImpl implements AccountService {
//    属性
    @Value("张三")
    private String username;

    private Integer age;
    private Date birthday;
//    @Autowired
//    @Qualifier("accountDaoImpl")
//    @Resource(name = "accountDaoImpl")
//    @Value("#{accountDaoImpl}")

    @Autowired
    private AccountDao accountDaoImpl;


    public AccountServiceImpl() {
        System.out.println("调用了无参构造...");
    }

    public AccountServiceImpl(String username, Integer age, Date birthday) {
        this.username = username;
        this.age = age;
        this.birthday = birthday;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public AccountDao getAccountDaoImpl() {
        return accountDaoImpl;
    }

    public void setAccountDaoImpl(AccountDao accountDaoImpl) {
        this.accountDaoImpl = accountDaoImpl;
    }

    //执行转账业务操作
    @Override
    public void save(String innername,String outtername,int money) {
        accountDaoImpl.innerMoney(innername,money);
//        System.out.println(5/0);
        accountDaoImpl.outterMoney(outtername, money);
    }

@PostConstruct
    public  void start(){
        System.out.println("我在容器初始化的时候会被调用");
    }
@PreDestroy
    public  void end(){
        System.out.println("我在容器销毁的时候会被调用");
    }


    @Override
    public String toString() {
        return "AccountServiceImpl{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", accountDaoImpl=" + accountDaoImpl+
                '}';
    }
}
